package bTree.LeetCode;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

public class 统计坏数对的数目 {
    public long countBadPairs(int[] nums) {
        long length = nums.length;
        Map<Long,Long>map=new HashMap<>();
        for (int i = 0; i < length; i++) {
            long key = i - nums[i];
            if (!map.containsKey(key)){
                map.put(key, 1L);
            }else {
                map.put(key,map.get(key)+1);
            }
        }
        long sum=length*(length-1)/2;
        AtomicLong path= new AtomicLong();
        map.entrySet().stream().forEach(a->{
            long value = a.getValue();
            path.addAndGet((value * (value - 1) / 2));
        });
        return sum- path.get();
    }
}